﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;
using TreasureJunk;

public partial class ReservationList : Page
{
    private object _routeList;

    private object _subRouteList;

    public object RouteList
    {
        get
        {
            if (_routeList == null)
            {
                _routeList = (HttpContext.Current.Cache["RouteList"] as List<object>);
                if (_routeList == null)
                {
                    _routeList = ParameterDAO.CreateInstant()
                        .SelectListItem(null, "Route", "", "", null, true, new HashSet<string>());
                    HttpContext.Current.Cache.Insert("RouteList", _routeList);
                }
            }
            return _routeList;
        }
        set
        {
            _routeList = value;
            HttpContext.Current.Cache.Insert("RouteList", _routeList);
        }
    }

    public object SubRouteList
    {
        get
        {
            if (_subRouteList == null)
            {
                _subRouteList = (HttpContext.Current.Cache["SubRouteList"] as List<object>);
                if (_subRouteList == null)
                {
                    _subRouteList = ParameterDAO.CreateInstant()
                        .SelectListItem(null, "SubRoute", "", "", null, true, new HashSet<string>());
                    HttpContext.Current.Cache.Insert("SubRouteList", _subRouteList);
                }
            }
            return _subRouteList;
        }
        set
        {
            _subRouteList = value;
            HttpContext.Current.Cache.Insert("SubRouteList", _subRouteList);
        }
    }

    /// <summary>
    ///     Page load Method
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<BoatEntity> ls = BoatDAO.CreateInstant().GetAllBoats();
            ddlBoat.DataSource = ls;
            ddlBoat.DataTextField = "BoatName";
            ddlBoat.DataValueField = "BoatID";
            ddlBoat.DataBind();

            ddlRoute.DataSource = RouteList;
            ddlRoute.DataTextField = "ShortValue";
            ddlRoute.DataValueField = "ShortValue";
            ddlRoute.DataBind();

            ddlSubRoute.DataSource = SubRouteList;
            ddlSubRoute.DataTextField = "ShortValue";
            ddlSubRoute.DataValueField = "ShortValue";
            ddlSubRoute.DataBind();

            //GetPropertyFromQueryString();
            //Search();
        }
        //btnExcel.Click += new EventHandler(btnExcel_Click);
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        string bookingCode = txtBookingCode.Text;
        string boatID = ddlBoat.Text;
        int status = Int32.Parse(ddlStatus.SelectedValue);
        int clientType = Int32.Parse(ddlClientType.SelectedValue);
        int routeNumber = Int32.Parse(ddlRoute.SelectedValue);

        DateTime dateTemp;
        DateTime? fromDepartureDate = null;
        DateTime? toDepartureDate = null;
        DateTime? fromCreatedDate = null;
        DateTime? toCreatedDate = null;

        if (DateTime.TryParse(txtFromDepDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault,
            out dateTemp)) fromDepartureDate = dateTemp;
        if (DateTime.TryParse(txtToDepDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault,
            out dateTemp)) toDepartureDate = dateTemp;
        if (DateTime.TryParse(txtFromCreatedDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault,
            out dateTemp)) fromCreatedDate = dateTemp;
        if (DateTime.TryParse(txtToCreatedDate.Text, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault,
            out dateTemp)) toCreatedDate = dateTemp;

        var grv = new GridView {AutoGenerateColumns = false};
        grv.HeaderStyle.BackColor = Color.FromName("#b10003");
        grv.HeaderStyle.ForeColor = Color.FromName("#f1e8df");
        grv.AlternatingRowStyle.BackColor = Color.FromName("#eee");
        grv.RowDataBound += grv_RowDataBound;
        grv.Columns.Add(new BoundField
        {
            HeaderText = @"Departure Date",
            DataField = "CheckInDate",
            DataFormatString = "{0:MM/dd/yyyy}"
        });
        grv.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        grv.Columns.Add(new BoundField {HeaderText = @"# of Pax", DataField = "NumOfPeple"});
        grv.Columns.Add(new BoundField {HeaderText = @"Route #", DataField = "RouteNumber"});
        grv.Columns.Add(new BoundField {HeaderText = @"Booking Code", DataField = "ReservationCode"});
        grv.Columns.Add(new BoundField {HeaderText = @"Agent#", DataField = "GuestID"});
        grv.Columns.Add(new BoundField {HeaderText = @"Email", DataField = "Email"});
        grv.Columns.Add(new BoundField {HeaderText = @"Client type", DataField = "GuestType"});
        grv.Columns.Add(new BoundField {HeaderText = @"Status", DataField = "StatusReservation"});
        grv.DataSource = new Get().ExportReservationList(
            bookingCode, boatID, status, clientType, routeNumber, null,
            fromDepartureDate, toDepartureDate,
            fromCreatedDate, toCreatedDate);
        grv.DataBind();

        ProcessIO.ExportExcel(this, grv, "account.xls");
    }

    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;
        switch (e.Row.Cells[6].Text)
        {
            case "0":
                e.Row.Cells[6].Text = @"Agent";
                break;
            case "1":
                e.Row.Cells[6].Text = @"WI";
                break;
            default:
                e.Row.Cells[6].Text = @"Unknown";
                break;
        }

        switch (e.Row.Cells[7].Text)
        {
            case "0":
                e.Row.Cells[7].Text = @"Inprogress";
                break;
            case "1":
                e.Row.Cells[7].Text = @"Cancelled";
                break;
            default:
                e.Row.Cells[7].Text = @"Unknown";
                break;
        }
    }
}